package edu.hawaii.halealohacli.jcev.command;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
import edu.hawaii.halealohacli.jcev.processor.CommandProcessor;

/**
 * Tests the functionality of the Help class which implements the Command interface.
 * @author Christopher Foo
 *
 */
public class TestHelp {
  
  /**
   * The CommandProcessor containing the Map of valid commands.
   */
  CommandProcessor processor = new CommandProcessor();
  
  /**
   * The Help object to perform tests on.
   */
  Help help = new Help(processor.getCommands());
  
  /**
   * Checks that the help string returned is the one that is expected.
   */
  @Test
  public void testHelpString() {
    assertTrue("Help string differs from the expected value.", 
               "help\nLists descriptions of all valid commands.".equals(help.getHelpString()));
  }

  /**
   * Tests the verifyArguments method of the Help class which returns true if the string
   * passed represents valid arguments for the quit command or false if the string is invalid.
   */
  @Test
  public void testVerifyArguments() {
    
    //Valid Arguments
    assertTrue("Empty string is a valid argument, but returned false.", help.verifyArguments(""));
    assertTrue("Whitespace is a valid argument, but return false.", help.verifyArguments("    "));
    
    //Invalid Arguments
    assertFalse("Non-empty string is an invalid argument, but returned true.",
                 help.verifyArguments("notempty"));
    assertFalse("Non-empty string with whitespace is an invalid argument, but returned true.", 
                 help.verifyArguments("  notempty "));
  }
  
  /**
   * Checks that the command executes successfully with valid arguments.
   */
  @Test
  public void testValidArguments() {
    
    //Empty String
    try {
      help.execute("", null);
    }
    catch (Exception e) {
      fail("Valid execution of help should not throw exception.");
    }
    
    //Whitespace
    try {
      help.execute("    \t", null);
    }
    catch (Exception e) {
      fail("Valid execution of help should not throw exception.");
    }
  }
  
  /**
   * Check that the execute method throws a generic Exception when passed an invalid argument.
   */
  @Test
  public void testInvalidArguments() {
    boolean caught = false;
    try {
      help.execute("notempty", null);
    }
    catch (Exception e) {
      caught = true;
    }
    
    assertTrue("Invalid arguments in help should cause execute to throw an exception.", caught);
  }
}
